﻿using System.IO.Compression;

namespace SharpZip.Net
{
    public class AesTextZipFile
    {
        public static void ExtractToDirectory(string ztxtFile, string targetDir, AesEncryptorKey key)
        {
            if (!File.Exists(ztxtFile))
            {
                throw new FileNotFoundException(ztxtFile);
            }

            if (!Directory.Exists(targetDir))
            {
                Directory.CreateDirectory(targetDir);
            }

            var ztxtBase64String = File.ReadAllText(ztxtFile);
            var ztxtBytes = Base64Encoding.GetBytes(ztxtBase64String);
            var encrytorZtxtBytes = AesEncryptor.Decrypt(ztxtBytes, key);

            using MemoryStream ztxtStream = new MemoryStream(encrytorZtxtBytes);

            ZipFile.ExtractToDirectory(ztxtStream, targetDir);
        }

        public static void CreateFromDirectory(string sourceDirectory, string targetZtxtFile, AesEncryptorKey key, string[]? ignorePaths = null)
        {
            using var stream = SharpZipFile.CreateFromDirectory(sourceDirectory, key, ignorePaths);
            var base64Content = Base64Encoding.GetString(stream.ToArray());
            File.WriteAllText(targetZtxtFile, base64Content);
        }
    }
}
